<?php

namespace app\admin\model;

use think\Model;

class CategoryAdmin extends Model
{
    public function userInfo()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
    public function cateInfo()
    {
        return $this->hasOne(Category::class, 'id', 'cate_id');
    }
    public static function getCateId($user_id)
    {
        return self::where(['user_id' => $user_id])->column('cate_id');
    }
    public static function getChildCateId($user_id)
    {
        $pid = self::getCateId($user_id);
        return Category::where('pid','in',$pid)->column('id');
    }
    public static function list($query = [])
    {
        $list = self::with(['userInfo' => function ($query) {
            $query->field('id,nickname');
        }, 'cateInfo' => function ($query) {
            $query->field('id,cate_name');
        }]);

        $search = [];
        foreach ($query as $item => $v) {
            if (!empty($v)) {
                $search[$item] = $v;
            }
        }
        if ($search) {
            $list->where($search);
        }
        $list = $list->paginate(10);
        return $list;
    }
}
